package cn.b.training.baby_education_training.api;

import cn.b.training.baby_education_training.entity.Encyclo;
import cn.b.training.baby_education_training.repository.EncycloRepository;
import cn.b.training.baby_education_training.repository.FoodTagRepository;
import cn.b.training.baby_education_training.repository.ScienceTagRepository;
import cn.b.training.baby_education_training.service.EncycloService;
import cn.b.training.baby_education_training.service.dto.EncycloDto;
import cn.b.training.baby_education_training.service.dto.EncyclopediaDto;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-02 11:58
 **/
@RestController
@RequestMapping("/encyclo")
public class EncycloController {
    @Autowired
    private EncycloService encycloService;
    @Autowired
    private EncycloRepository encycloRepository;
    @Autowired
    private FoodTagRepository foodTagRepository;
    @Autowired
    private ScienceTagRepository scienceTagRepository;

    @PostMapping
    public ResponseEntity addEncyclo(@Valid EncycloDto encyclo){
        try {
            boolean b = encycloService.addEncyclo(encyclo);
            if (b){
                JSONObject result = new JSONObject();
                result.put("success","添加成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","添加失败");
                return ResponseEntity.status(400).body(error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping
    public ResponseEntity getEncycloById(Long id) {
        try {
            EncyclopediaDto encyclo = this.encycloService.getEncycloById(id);
            return ResponseEntity.ok(encyclo);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/title")
    public ResponseEntity getEncycloByTitle(String eneycloTitle) {
        try {
            String newEncycloTitle = "%"+eneycloTitle+"%";
            List<Encyclo> encyclo = this.encycloService.getEncycloByTitle(newEncycloTitle);
            return ResponseEntity.ok(encyclo);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error", e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @DeleteMapping
    public ResponseEntity DeleteEncycloById(Long id){
        try {
            boolean b = encycloService.deleteEncycloById(id);
            if (b) {
                JSONObject result = new JSONObject();
                result.put("success", "删除成功");
                return ResponseEntity.ok(result);
            }else {
                JSONObject error = new JSONObject();
                error.put("failed","删除失败");
                return ResponseEntity.status(400).body(error);
            }
        }catch (Exception e){
            e.printStackTrace();
            JSONObject error = new JSONObject();
            error.put("error",e.getMessage());
            return ResponseEntity.status(400).body(error);
        }
    }

    @GetMapping("/all")
    public ResponseEntity getEncyclos(@RequestParam(required = false, defaultValue = "0") Integer page,
                                      @RequestParam(required = false, defaultValue = "10") Integer size) {
        Page<Encyclo> result = this.encycloService.getAllEncyclos(page, size);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/food/all")
    public ResponseEntity getFoods() {
        return ResponseEntity.ok(foodTagRepository.findAll());
    }
    @GetMapping("/science/all")
    public ResponseEntity getScience() {
        return ResponseEntity.ok(scienceTagRepository.findAll());
    }

    @GetMapping("/foodTag")
    public ResponseEntity getEncycloByFoodId(@RequestParam Long foodTagId){
        return ResponseEntity.ok(encycloRepository.getByFoodId(foodTagId));
    }

    @GetMapping("/scienceTag")
    public ResponseEntity getEncycloByScienceId(@RequestParam Long scienceTagId){
        return ResponseEntity.ok(encycloRepository.getByScienceId(scienceTagId));
    }
}
